class Solution
{
public:
    int getNumberOfBacklogOrders(vector<vector<int>> &orders)
    {
        auto compareSell = [](auto &order1, auto &order2)
        {
            return order1[0] > order2[0];
        };
        auto compareBuy = [](auto &order1, auto &order2)
        {
            return order1[0] < order2[0];
        };
        using SellOrderQueue = priority_queue<vector<int>, vector<vector<int>>, decltype(compareSell)>;
        using BuyOrderQueue = priority_queue<vector<int>, vector<vector<int>>, decltype(compareBuy)>;
        SellOrderQueue sellOrders(compareSell);
        BuyOrderQueue buyOrders(compareBuy);

        constexpr int BUY = 0;
        constexpr int SELL = 1;
        for (auto &order : orders)
        {
            if (order[2] == BUY)
            {
                while (!sellOrders.empty())
                {
                    auto topOrder = sellOrders.top();
                    sellOrders.pop();
                    if (topOrder[0] <= order[0])
                    {
                        if (topOrder[1] <= order[1])
                        {
                            order[1] -= topOrder[1];
                            if (order[1] == 0)
                            {
                                break;
                            }
                        }
                        else
                        {
                            topOrder[1] -= order[1];
                            sellOrders.push(topOrder);
                            order[1] = 0;
                            break;
                        }
                    }
                    else
                    {
                        sellOrders.push(topOrder);
                        break;
                    }
                }
                if (order[1] != 0)
                {
                    buyOrders.push(order);
                }
            }
            else
            {
                while (!buyOrders.empty())
                {
                    auto topOrder = buyOrders.top();
                    buyOrders.pop();
                    if (topOrder[0] >= order[0])
                    {
                        if (topOrder[1] <= order[1])
                        {
                            order[1] -= topOrder[1];
                            if (order[1] == 0)
                            {
                                break;
                            }
                        }
                        else
                        {
                            topOrder[1] -= order[1];
                            buyOrders.push(topOrder);
                            order[1] = 0;
                            break;
                        }
                    }
                    else
                    {
                        buyOrders.push(topOrder);
                        break;
                    }
                }
                if (order[1] != 0)
                {
                    sellOrders.push(order);
                }
            }
        }
        long long result = 0;
        while (!buyOrders.empty())
        {
            auto order = buyOrders.top();
            buyOrders.pop();
            result += order[1];
            result %= 1000000007;
        }
        while (!sellOrders.empty())
        {
            auto order = sellOrders.top();
            sellOrders.pop();
            result += order[1];
            result %= 1000000007;
        }
        return result;
    }
};